package com.seagox.desk.remote.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.seagox.desk.remote.entity.BaseUser;
import com.seagox.desk.remote.mapper.UserMapper;
import com.seagox.desk.remote.service.IUserService;
import com.seagox.desk.common.ResultCode;
import com.seagox.desk.common.ResultData;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService implements IUserService {
    
    @Autowired
    private UserMapper userMapper;
	
	@Override
	public ResultData connect(String identifyCode, String validateCode) {
		LambdaQueryWrapper<BaseUser> qw = new LambdaQueryWrapper<>();
		qw.eq(BaseUser::getIdentifyCode, identifyCode).eq(BaseUser::getValidateCode, validateCode);
		BaseUser baseUser = userMapper.selectOne(qw);
		if(baseUser != null) {
			if(baseUser.getStatus().equals(0)) {
				return ResultData.warn(ResultCode.OTHER_ERROR, "用户不在线");
			} else {
				return ResultData.success(baseUser);
			}
		} else {
			return ResultData.warn(ResultCode.OTHER_ERROR, "识别码或者验证码错误");
		}
	}

	@Override
	public ResultData updateValidateCode(String userId, String validateCode) {
		BaseUser user = userMapper.selectById(userId);
		user.setValidateCode(validateCode);
		userMapper.updateById(user);
		return ResultData.success(null);
	}
}
